package com.zhongshengnetwork.rightbe.keyboard.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.zhongshengnetwork.rightbe.Constant.APIKey;
import com.zhongshengnetwork.rightbe.CustomApplication;
import com.zhongshengnetwork.rightbe.R;
import com.zhongshengnetwork.rightbe.common.AppThemeUtils;
import com.zhongshengnetwork.rightbe.common.BroadcastDefine;
import com.zhongshengnetwork.rightbe.keyboard.data.AppBean;
import com.zhongshengnetwork.rightbe.keyboard.data.PicBean;

import java.util.ArrayList;

/**
 * Created by zhongsheng-luojianlan on 17/1/2.
 */

public class PicAdapter extends BaseAdapter {

    private LayoutInflater inflater;
    private Context mContext;
    private ArrayList<PicBean> mDdata = new ArrayList<PicBean>();

    public PicAdapter(Context context, ArrayList<PicBean> data) {
        this.mContext = context;
        this.inflater = LayoutInflater.from(context);
        if (data != null) {
            this.mDdata = data;
        }
    }

    @Override
    public int getCount() {
        return mDdata.size();
    }

    @Override
    public Object getItem(int position) {
        return mDdata.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            viewHolder = new ViewHolder();
            convertView = inflater.inflate(R.layout.item_pic, null);
            viewHolder.iv_icon = (ImageView) convertView.findViewById(R.id.pic_icon);
            viewHolder.iv_name = (TextView) convertView.findViewById(R.id.pic_name);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        final PicBean picBean = mDdata.get(position);
        if (picBean != null) {
            String url = picBean.getHost()+picBean.getName()+"small"+picBean.getId()+".jpg";
            Glide.with(mContext).load(url).placeholder(AppThemeUtils.getInstance().getDefaultImageBg()).dontAnimate().into(viewHolder.iv_icon);
            viewHolder.iv_name.setText(picBean.getTitle());
            convertView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent mIntent = new Intent(BroadcastDefine.SendPicMsg);
                    //发送广播
                    String content = picBean.getHost()+picBean.getName()+picBean.getId()+".jpg";
                    mIntent.putExtra(APIKey.contentKey,content);
                    CustomApplication.mContext.sendBroadcast(mIntent);
                }
            });
        }
        return convertView;
    }

    class ViewHolder {
        public ImageView iv_icon;
        public TextView iv_name;
    }
}
